package daoJDBC;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import connection.ConnectionDB;
import entityJDBC.Candidate;
import entityJDBC.CandidateComment;
import entityJDBC.User;

public class JDBCCandidateCommentDAO/* implements ICandidateCommentDAO */{

	ConnectionDB conn = ConnectionDB.getInstance();

	public JDBCCandidateCommentDAO() {

	}

	/*@Override*/
	public void insert(CandidateComment candidateComment) {
		try {
			String sql = "insert into candidatecomment(candidatecomment,idUser,idCandidate) values(?,?,?)";
			PreparedStatement preparedStatement = conn.getConnection()
					.prepareStatement(sql);
			preparedStatement.setString(1,
					candidateComment.getCandidateComment());
			preparedStatement.setInt(2, candidateComment.getUser().getIdUser());
			preparedStatement.setInt(3, candidateComment.getCandidate()
					.getIdCanditate());
			preparedStatement.executeUpdate();
		} catch (SQLException e) { // TODO: handle exception
			System.out.println(e.toString());
		} finally {
			// conn.disconectDatabase();
		}

	}

	/*@Override*/
	public void update(CandidateComment candidateComment) {
		try {
			String sql = "update candidatecomment set candidateComment='"
					+ candidateComment.getCandidateComment()
					+ "' where idCandidateComment="
					+ candidateComment.getIdCandidateComment();
			Statement statement = conn.getConnection().createStatement();
			statement.executeUpdate(sql);
		} catch (SQLException e) { // TODO: handle exception
			System.out.println(e.toString());
		} finally {
			// conn.disconectDatabase();
		}

	}

	/*@Override*/
	public void delete(CandidateComment candidateComment) {
		try {
			String sql = "delete from candidatecomment where idCandidateComment="
					+ candidateComment.getIdCandidateComment();
			Statement statement = conn.getConnection().createStatement();
			statement.executeUpdate(sql);
		} catch (SQLException e) { // TODO: handle exception
			System.out.println(e.toString());
		} finally {
			// conn.disconectDatabase();
		}

	}

	/*@Override*/
	public CandidateComment findById(int idCandidateComment) {
		CandidateComment candidateComment;
		try {
			String sql = "select * from javatr.candidatecomment where idcandidatecomment="
					+ idCandidateComment;

			Statement statement = conn.getConnection().createStatement();
			ResultSet result = statement.executeQuery(sql);

			if (result.next()) {
				String candidateComm = result.getString("candidatecomment");
				int idUser = result.getInt("idUser");
				JDBCUserDao jdbcu = new JDBCUserDao();
				User user = jdbcu.findById(idUser);
				int idCandidate = result.getInt("idCandidate");
				JDBCCandidateDAO jdbcc = new JDBCCandidateDAO();
				Candidate candidate = jdbcc.findById(idCandidate);
				candidateComment = new CandidateComment(idCandidateComment,
						candidateComm, user, candidate);
				return candidateComment;
			}
		} catch (SQLException e) { // TODO: handle exception
			System.out.println(e.toString());
		} finally {
			// conn.disconectDatabase();
		}
		return null;
	}

	/*@Override*/
	public List<CandidateComment> findAll() {
		List<CandidateComment> comments = new ArrayList<CandidateComment>();
		try {
			String sql = "select * from javatr.candidatecomment";
			Statement statement = conn.getConnection().createStatement();
			ResultSet result = statement.executeQuery(sql);
			while (result.next()) {
				int idCandidateComment = result.getInt("idcandidatecomment");
				String candidateComm = result.getString("candidatecomment");
				int idUser = result.getInt("idUser");
				JDBCUserDao jdbcu = new JDBCUserDao();
				User user = jdbcu.findById(idUser);
				int idCandidate = result.getInt("idCandidate");
				JDBCCandidateDAO jdbcc = new JDBCCandidateDAO();
				Candidate candidate = jdbcc.findById(idCandidate);
				CandidateComment candidateComment = new CandidateComment(
						idCandidateComment, candidateComm, user, candidate);
				comments.add(candidateComment);
			}
			return comments;

		} catch (SQLException e) { // TODO: handle exception
			System.out.println(e.toString());
		} finally {
			// conn.disconectDatabase();
		}
		return null;
	}
}
